package tuc.lagerplan.gui.artikelbrowser;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.lagerplan.basisdienste.stammdaten.persistence.ArtikelDAO;
import com.lagerplan.main.*;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class _ArtikelBrowser extends org.eclipse.swt.widgets.Composite {
	private TableViewer artikelTableViewer;
	
	// nachfolgend siehe Abgleich mit ArtikelDAO
	public static final int ARTIKEL_ID = 0;
	public static final int ARTIKELNAME = 1;
	public static final int HUELLGEOMETRIE = 2;
	public static final int LAENGE = 3;
	public static final int BREITE = 4;
	public static final int HOEHE = 5;
	public static final int DURCHMESSER = 6;
	public static final int MASSE = 7;
	public static final int STAPELBAR = 8;
	public static final int STAPELFACTOR = 9;
	public static final int STAPELHOEHE = 10;
	public static final int UMSCHLIESSUNG = 11;
	public static final int LOSGROESSE = 12;
	public static final int MELDEMENGE = 13;
	public static final int MAXIMALBESTAND = 14;
	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		try {
			LagerplanCommand.initialize();
			showGUI();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		ArtikelBrowser inst = new ArtikelBrowser(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public _ArtikelBrowser(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FillLayout thisLayout = new FillLayout(org.eclipse.swt.SWT.HORIZONTAL);
			this.setLayout(thisLayout);
			{
				artikelTableViewer = new TableViewer(this, SWT.NONE);
				artikelTableViewer.setContentProvider(new ArtikelBrowserContentProvider());
				artikelTableViewer.setLabelProvider(new ArtikelBrowserLabelProvider());
				
				artikelTableViewer.getTable().setHeaderVisible(true);
				artikelTableViewer.getTable().setLinesVisible(true);
				setupTable();
				artikelTableViewer.setInput( ArtikelDAO.getArtikel() );
				
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Legt den Tabellenkopf an
	 * Tabelle muss existieren!
	 * Siehe Abgleich mit ArtikelDAO und Def. Indexkonstanten. Reihenfolge muss mit Index uebereinstimmen!
	 */
	private void setupTable(){
		
		if(null!=artikelTableViewer){
			Table table = artikelTableViewer.getTable();
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.ARTIKEL_ID);
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.ARTIKELNAME);
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.HUELLGEOMETRIE);
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.LAENGE);
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.BREITE);
			new TableColumn(table, SWT.LEFT).setText(ArtikelDAO.HOEHE);
			
			//configure the table for display
			TableLayout layout = new TableLayout();
			layout.addColumnData(new ColumnWeightData(33, true));
			layout.addColumnData(new ColumnWeightData(33, true));
			layout.addColumnData(new ColumnWeightData(33, true));
			table.setLayout(layout);
		}
	}
	
	public TableViewer getArtikelTableViewer(Composite parent) {
		return artikelTableViewer;
	}
}
